import {export_json_to_excel} from './Export2Excel'

// 获取-表格头部标题数据
function getHeaderAnd({enumData}) {
  let header = [], filterVal = [];
  for (let key in enumData) {
    header.push(enumData[key]);
    filterVal.push(key);
  }

  return {
    header,
    filterVal
  }
}

// 格式化数据
function formatJson({data}, filterVal) {
  return data.map(v => filterVal.map(j => v[j]));
}

function getMultiHeader({filename}, header) {
  let len = header.length - 1;
  let headerArr = [[]];
  for (let i = 0; i < len; i++) {
    headerArr[0].push('');
  }
  headerArr[0].unshift(filename);
  return headerArr;
}

// 导出excel
export function exportExcel(options = {}) {
  const {header, filterVal} = getHeaderAnd(options);
  let multiHeader = getMultiHeader(options, filterVal);
  let data = formatJson(options, filterVal);
  export_json_to_excel({
    multiHeader,
    header,
    data,
    filename: options.filename,
  });
}






